package TCP回显服务器;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TCPClient {
    Socket socket=null;
    public TCPClient() throws IOException {
        socket=new Socket("127.0.0.1",8000);
    }
    public void start() throws IOException {
        Scanner scanner=new Scanner(System.in);
        try(InputStream inputStream=socket.getInputStream(); OutputStream outputStream=socket.getOutputStream()){
            Scanner scannerNext=new Scanner(inputStream);
            PrintWriter printWriter=new PrintWriter(outputStream);
            while (true){
                System.out.print("> ");
                //构造请求
                String request = scanner.next();
                //将请求发送给服务器
                printWriter.println(request);
                printWriter.flush();
                //捕捉响应
                String response=scannerNext.next();
                //显示响应结果到控制台
                System.out.printf("req:%s;resp:%s\n",request,response);
            }
        }
    }
    public static void main(String[] args) throws IOException {
        TCPClient client = new TCPClient();
        client.start();
    }
}
